import time
import traceback
import sys
from zipfile import ZipFile

class ZipUtil:
    def __init__(self, zipFile):
        """
        @param zipFile: a zip file path or a file like object
        """     
        self.zip = ZipFile(zipFile)
        self.res = []
    
    def getFileContentWithExtension(self,ext):
        """
        @param ext: the file extensions to look for
        @return: a list of file content data
        """       
        retList = []
        for info in self.zip.infolist():
            name = info.filename
            index = name.rfind(".")
            if index != -1:
                if name[len(name)-len(ext):len(name)].lower() == ext:                    
                    data = self.zip.read(name)
                    tmp = [name, data]
                    retList.append(tmp)        
        return retList
    
    def getAllFileContents(self):
        """
        @summary:
        @return:
        """
        retList = []
        for info in self.zip.infolist():
            name = info.filename                
            data = self.zip.read(name)
            tmp = [name, data]
            retList.append(tmp)        
        return retList
      
    
if __name__ == "__main__":
    #f = file(r"c:\!Projects\NSIF\ISO19115p2xsd-20080407.zip","rb")    
    
    util = ZipUtil(r"c:\!Projects\NSIF\ISO19115p2xsd-20080407.zip")
    print util.getFileContentWithExtension("xsd")
#    zFile = ZipFile(f)
#    #zFile = zipfile.ZipFile(r"c:\!Projects\NSIF\ISO19115p2xsd-20080407.zip")
#    for info in zFile.infolist():
#        print info.filename
#        #data = zFile.read(info.filename)        
#        #print data
#        #print "-"*100
    
        
        